package com.springboot.shanhexin.controller;

import com.springboot.shanhexin.entity.Page;
import com.springboot.shanhexin.entity.scenic_spot;
import com.springboot.shanhexin.service.scenic_spotService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class scenic_spotCtrl {

    @Autowired
    private scenic_spotService scenic_spotService;

    //分页显示全部
    @RequestMapping("/scenic_spot")
    public String scenic_spot(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = scenic_spotService.getCount();
        List<scenic_spot> scenic_spotList=new ArrayList<scenic_spot>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            scenic_spotList=scenic_spotService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            scenic_spotList=scenic_spotService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_scenic_spot");
        model.addAttribute("show_left", "left_scenic_spot");
        model.addAttribute("left", "left_y_scenic_spot");
        model.addAttribute("mypage", "scenic_spot");
        model.addAttribute("scenic_spot", scenic_spotList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_scenic_spot";
    }

    //分页显示全部搜索
    @RequestMapping("/searchscenic_spot")
    public String searchscenic_spot(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        totalCount = scenic_spotService.getsearchCount(keyword);
        List<scenic_spot> scenic_spotList=new ArrayList<scenic_spot>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            scenic_spotList=scenic_spotService.findsearchByPage(keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            scenic_spotList= scenic_spotService.findsearchByPage(keyword,page.getStartPos(), page.getPageSize());
        }
        if(scenic_spotList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_scenic_spot");
            model.addAttribute("show_left", "left_scenic_spot");
            model.addAttribute("left", "left_y_scenic_spot");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("scenic_spot", scenic_spotList);
            return "houtai_scenic_spot";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/scenic_spot?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/addscenic_spot")
    @ResponseBody
    public String addscenic_spot(scenic_spot scenic_spot) {
        scenic_spotService.add(scenic_spot);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/updatescenic_spot")
    public String updatescenic_spot(scenic_spot scenic_spot) {
        scenic_spotService.update(scenic_spot);
        return "redirect:/scenic_spot";
    }

    //删除
    @RequestMapping("/deletescenic_spot")
    public String deletescenic_spot(int userid) {
        scenic_spotService.delete(userid);
        return "redirect:/scenic_spot";
    }

    //跳转到更改页面
    @RequestMapping("/to_updatescenic_spot")
    public String to_updatescenic_spot(Model model, int userid) {
        List<scenic_spot> scenic_spotList=new ArrayList<scenic_spot>();
        scenic_spotList = scenic_spotService.getUserone(userid);
        model.addAttribute("top", "top_scenic_spot");
        model.addAttribute("show_left", "left_updatescenic_spot");
        model.addAttribute("left", "left_y_scenic_spot");
        model.addAttribute("mypage", "scenic_spot");
        model.addAttribute("scenic_spot", scenic_spotList);
        return "houtai_updatescenic_spot";
    }

    //跳转到添加页面
    @RequestMapping("/to_addscenic_spot")
    public String to_addscenic_spot(Model model) {
        model.addAttribute("top", "top_scenic_spot");
        model.addAttribute("show_left", "left_addscenic_spot");
        model.addAttribute("left", "left_y_scenic_spot");
        model.addAttribute("mypage", "scenic_spot");
        return "houtai_addscenic_spot";
    }

}
